package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"net/http"
	"strings"
	"ubt-aio-service/app/mod/exam_mod"
	"ubt-aio-service/app/pkg/code"
	"ubt-aio-service/app/pkg/conf"
	"ubt-aio-service/app/pkg/db"
	"ubt-aio-service/app/pkg/logger"
	"ubt-aio-service/app/pkg/route"
	"ubt-aio-service/app/serv/exam_serv"
	"ubt-aio-service/app/utils"
)

// main1 入口方法
func main() {
	fmt.Println("开始启动应用......")
	// 初始化配置文件的读取
	conf.InitConf("/home/ubt/apps/ubt-aio-service/conf/conf.json")
	// 初始化日志组件
	logger.InitLog(conf.Cfg.AppConf.LogPath)
	// 初始化Redis数据库
	db.InitRedis()
	// 初始化PostgreSQL数据库
	db.InitPgsql()
	// 启动服务
	fmt.Println("应用启动成功～！端口为：" + conf.Cfg.AppConf.AppPort)
	if err := http.ListenAndServe(":"+conf.Cfg.AppConf.AppPort, &route.RouteHandler{}); err != nil {
		fmt.Println("Server start failed!")
	}
}

// main2 代码生成方法
func main2() {
	// 1、定义需要的变量，注意大小写，根据实际业务表进行修改
	var (
		tableName  = "goods_spu" // 数据库表名
		packName   = "goods"     // 包名
		fileName   = "spu"       // 文件名
		className  = "Spu"       // 类名
		headName   = "Spu"       // 首字母大写名
		StructName = "spu"       // 结构体名
	)

	// 2、生成代码
	utils.GenCode(tableName, packName, className, StructName, fileName, headName)
}

// main3 导入题库
func main3() {
	qstList, optList := importExam()
	if qstList != nil {
		// 初始化配置文件的读取
		conf.InitConf("/home/ubt/apps/ubt-aio-service/conf/conf.json")
		// 初始化日志组件
		logger.InitLog(conf.Cfg.AppConf.LogPath)
		// 初始化PostgreSQL数据库
		db.InitPgsql()
		var ch = make(chan int)
		exam_serv.QstServ.AddQsts(qstList)
		go exam_serv.QstServ.AddOpts(ch, optList)
		ch <- 1
	}
}

// importExam 导入题库
func importExam() ([]*exam_mod.QstSaveReq, []*exam_mod.OptSaveReq) {
	f, er := excelize.OpenFile("/home/ubt/apps/ubt-aio-service/file/upload/exam.xlsx")
	if er != nil {
		logger.Error("读取的文件失败")
	}
	var (
		rows    [][]string
		qstId   string
		qstList []*exam_mod.QstSaveReq
		qstVo   *exam_mod.QstSaveReq
		optList []*exam_mod.OptSaveReq
		optVo   *exam_mod.OptSaveReq
		optId   string
	)
	// 获取 单选题 上所有单元格
	rows, _ = f.GetRows("单选题")
	for _, row := range rows {
		qstId = code.NewNanoid(21)
		qstVo = &exam_mod.QstSaveReq{
			QstId:     qstId,
			Kind:      1,
			QstNo:     row[0],
			Title:     row[1],
			Answer:    "",
			Keywords:  row[6],
			FileNames: "",
			FileUrls:  "",
			Remarks:   "",
		}

		if row[2] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "A",
				OptName:  row[2],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if row[6] == "A" {
				optVo.IzRight = 1
				qstVo.Answer = optId
			}
			optList = append(optList, optVo)
		}
		if row[3] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "B",
				OptName:  row[3],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if row[6] == "B" {
				optVo.IzRight = 1
				qstVo.Answer = optId
			}
			optList = append(optList, optVo)
		}
		if row[4] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "C",
				OptName:  row[4],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if row[6] == "C" {
				optVo.IzRight = 1
				qstVo.Answer = optId
			}
			optList = append(optList, optVo)
		}
		if row[5] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "D",
				OptName:  row[5],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if row[6] == "D" {
				optVo.IzRight = 1
				qstVo.Answer = optId
			}
			optList = append(optList, optVo)
		}

		qstList = append(qstList, qstVo)
	}
	// 获取 判断题 上所有单元格
	rows, _ = f.GetRows("判断题")
	for _, row := range rows {
		qstId = code.NewNanoid(21)
		qstVo = &exam_mod.QstSaveReq{
			QstId:     qstId,
			Kind:      2,
			QstNo:     row[0],
			Title:     row[1],
			Answer:    "",
			Keywords:  row[4],
			FileNames: "",
			FileUrls:  "",
			Remarks:   "",
		}

		optId = code.NewNanoid(21)
		optVo = &exam_mod.OptSaveReq{
			OptId:    optId,
			QstId:    qstId,
			OptNo:    "A",
			OptName:  "正确",
			ImgNames: "",
			ImgUrls:  "",
			IzRight:  2,
			Answer:   "",
			Keywords: "",
			Remarks:  "",
		}
		if row[4] == "A" {
			optVo.IzRight = 1
			qstVo.Answer = optId
		}
		optList = append(optList, optVo)
		optId = code.NewNanoid(21)
		optVo = &exam_mod.OptSaveReq{
			OptId:    optId,
			QstId:    qstId,
			OptNo:    "B",
			OptName:  "错误",
			ImgNames: "",
			ImgUrls:  "",
			IzRight:  2,
			Answer:   "",
			Keywords: "",
			Remarks:  "",
		}
		if row[4] == "B" {
			optVo.IzRight = 1
			qstVo.Answer = optId
		}
		optList = append(optList, optVo)

		qstList = append(qstList, qstVo)
	}
	// 获取 多选题 上所有单元格
	rows, _ = f.GetRows("多选题")
	for _, row := range rows {
		qstId = code.NewNanoid(21)
		qstVo = &exam_mod.QstSaveReq{
			QstId:     qstId,
			Kind:      3,
			QstNo:     row[0],
			Title:     row[1],
			Answer:    "",
			Keywords:  row[9],
			FileNames: "",
			FileUrls:  "",
			Remarks:   "",
		}
		answers := ""

		if row[2] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "A",
				OptName:  row[2],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if strings.Contains(row[9], "A") {
				optVo.IzRight = 1
				answers += optId + ","
			}
			optList = append(optList, optVo)
		}
		if row[3] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "B",
				OptName:  row[3],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if strings.Contains(row[9], "B") {
				optVo.IzRight = 1
				answers += optId + ","
			}
			optList = append(optList, optVo)
		}
		if row[4] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "C",
				OptName:  row[4],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if strings.Contains(row[9], "C") {
				optVo.IzRight = 1
				answers += optId + ","
			}
			optList = append(optList, optVo)
		}
		if row[5] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "D",
				OptName:  row[5],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if strings.Contains(row[9], "D") {
				optVo.IzRight = 1
				answers += optId + ","
			}
			optList = append(optList, optVo)
		}
		if row[6] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "E",
				OptName:  row[6],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if strings.Contains(row[9], "E") {
				optVo.IzRight = 1
				answers += optId + ","
			}
			optList = append(optList, optVo)
		}
		if row[7] != "" {
			optId = code.NewNanoid(21)
			optVo = &exam_mod.OptSaveReq{
				OptId:    optId,
				QstId:    qstId,
				OptNo:    "F",
				OptName:  row[7],
				ImgNames: "",
				ImgUrls:  "",
				IzRight:  2,
				Answer:   "",
				Keywords: "",
				Remarks:  "",
			}
			if strings.Contains(row[9], "F") {
				optVo.IzRight = 1
				answers += optId + ","
			}
			optList = append(optList, optVo)
		}
		qstVo.Answer = answers

		qstList = append(qstList, qstVo)
	}
	return qstList, optList
}
